<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>员工工时统计</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/build/css/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/my-style.css">
</head>
<style>
    .laydate-btns-clear {
        display: none !important;
    }
</style>
<body>
<div class="kit-doc">

    <!--表格搜索框组-->
    <div class="layui-form-item layui-formgroup layui-tab-card">

        <!-- 下拉框-->
        <div class="layui-inline">
            <form class="layui-form " action="">
                <label class="layui-form-label">请选择统计条件：</label>
                <div class="layui-input-inline">
                    <select name="interest" id="searchProject" lay-verify="required" lay-search="">
                        <option  value="">项目</option>
                        <option value="" th:each="project : ${projectList}" th:value="${project.getId()}"
                                th:text="${project.getProjectName()}"></option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="name" placeholder="员工姓名">
                </div>
                <div class="layui-input-inline">
                    <input type="number" class="layui-input" id="num" placeholder="节假日天数">
                </div>
                <div class="layui-input-inline">
                    <select name="interest" id="status" lay-filter="aihao">
                        <option value="">状态</option>
                        <option value="0">正常</option>
                        <option value="1">异常</option>
                    </select>
                </div>
                <!-- 时间 -->
                <div class="layui-inline">
                    <input type="hidden" id="mon">
                    <input type="hidden" id="sun">
                    <div class="layui-input-inline">
                        <input class="layui-input" id="test6" placeholder=" 时间段 " type="text">
                    </div>
                </div>
            </form>

        </div>
        <!-- 按钮 -->
        <button class="layui-btn layui-btn-sm" id="search">统计</button>
        <button class="layui-btn layui-btn-sm" id="export">导出</button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="preWeekBtn">
            <i class="fa fa-reply margin-r3"></i>上一周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="thisWeekBtn">
            <i class="fa fa-search margin-r3"></i>本周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="nextWeekBtn">
            <i class="fa fa-share margin-r3"></i>下一周
        </button>

    </div>

    <div class="layui-card layui-card-body">
        <table class="layui-table xuhao-table" id="table-context" lay-filter="test">
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            </script>
        </table>
    </div>

</div>
<!--状态模板-->
<script type="text/html" id="statusTpl">
    {{# if(d.shouldHour <= d.realworkhour){ }}
    <a class="layui-btn layui-btn-xs">正常</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-xs" style="background-color: red">异常</a>
    {{# }  }}
</script>
<!-- .js-->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script src="/plugins/layui/layui.js"></script>
<script src="/js/workhour.js"></script>
<script src="/js/ry-ui.js"></script>
<script>
    var projectToName = function (projectId) {
        return layui.$("#searchProject option[value=" + projectId + "]").text();
    };
</script>
<script type="text/html" id="projectTpl">
    {{ projectToName(d.projectId) }}
</script>
<script>
    var now = new Date();

    // 获取周一和周末
    var start = getTime(0, now);
    var end = getTime(-6, now);
    $("#mon").val(start);
    $("#sun").val(end);
    $("#test6").val(start + ' - ' + end);
</script>
<script>
    layui.use(['table', 'jquery'], function () {
        var table = layui.table;
        var $ = layui.$;
        var projectTable = table.render({
            elem: '#table-context'
            , url: '/statistics/user/list'
            , height: 'full-200'
            , page: true
            , limit: 10
            , limits: [10, 20, 30]
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , cols: [
                [
                    // {type: 'checkbox'},
                    {field: 'username', title: '姓名', align: 'center',width: '15%'},
                    {field: 'shouldHour', title: '应有工时', align: 'center',width: '20%'},
                    {field: 'workhour', title: '填报工时', sort: true,align: 'center',width: '20%'},
                    {field: 'realworkhour', title: '已审批工时', sort: true,align: 'center',width: '20%'},
                    {field: 'statue', title: '是否异常', templet: '#statusTpl',align: 'center',width: '10%'}
                    , {fixed: 'right', title: "操作", align: 'center', toolbar: '#barDemo',width: '15%'}
                ]
            ],
            done: function (res, curr, count) {
            }
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                var startToEnd = $("#test6").val().split(" - ");
                $.modal.open("详情", "/statistics/user/view/" + data.username + "?startDate=" + startToEnd[0] + "&endDate=" + startToEnd[1])
            }
        });

        /**
         * 刷新表格数据
         */
        function reloadData() {
            var startToEnd = $("#test6").val().split(" - ");
            var name = $("#name").val();
            var status = $("#status").val();
            var num = $("#num").val();
            // console.log(startToEnd,projectId,projectName,typeName);
            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    name: name,
                    status:status,
                    num:num == "" ? 2 : parseInt(num),// 如果节假日天数没设置，默认为2天
                    startDate: startToEnd[0],
                    endDate: startToEnd[1],
                    proId: $("#searchProject").val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }

        $("#search").click(function () {
            reloadData();
        });

        // 导出
        $("#export").click(function () {
            var startToEnd = $("#test6").val().split(" - ");
            var name = $("#name").val();
            var status = $("#status").val();
            var num = $("#num").val();
            window.location.href = "/statistics/user/excel?" +
                "name=" + name +
                "&num=" + num +
                "&status=" + status +
                "&startDate=" + startToEnd[0] +
                "&endDate=" + startToEnd[1];
        });

        // 上一周
        $("#preWeekBtn").click(function () {
            var monDate = $("#mon").val();
            var preMon = dateToStr(addDate(monDate, -7));
            var preSun = dateToStr(addDate(monDate, -1));
            $("#mon").val(preMon);
            $("#sun").val(preSun);
            $("#test6").val(preMon + ' - ' + preSun);
            reloadData();
        });

        // 下一周
        $("#nextWeekBtn").click(function () {
            var monDate = $("#mon").val();
            var nextMon = dateToStr(addDate(monDate, 7));
            var nextSun = dateToStr(addDate(monDate, 13));
            $("#mon").val(nextMon);
            $("#sun").val(nextSun);
            $("#test6").val(nextMon + ' - ' + nextSun);
            reloadData();
        });

        // 本周一周
        $("#thisWeekBtn").click(function () {
            var now = new Date();
            // 获取周一和周末
            var start = getTime(0, now);
            var end = getTime(-6, now);
            $("#mon").val(start);
            $("#sun").val(end);
            $("#test6").val(start + ' - ' + end);
            reloadData();
        });
    });
</script>


</body>
</html>